package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.dto.PmsProductCategoryWithChildrenItem;
import cn.zcscloud.mall.mbg.model.PmsProductCategory;
import cn.zcscloud.mall.service.PmsProductCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: productCategoryController
 * Description:
 * date: 2022/4/3 13:57
 *
 * @author zjf95
 * @since JDK 1.8
 */
@Slf4j
@RestController
@RequestMapping("productCategory")
public class productCategoryController {


    @Autowired
    private PmsProductCategoryService pmsProductCategoryService;

    @GetMapping("/list/{parentId}")
    public R list(@PathVariable Integer parentId,int pageNum,int pageSize){

        log.info("数据：{}", parentId);
        log.info("分页的数据为：{}",pageNum+"===="+pageSize);

        Page<PmsProductCategory> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(parentId != null,PmsProductCategory::getParentId,parentId);

        pmsProductCategoryService.page(page,queryWrapper);

        return R.success("操作成功", PageRequest.init(page));
    }

    /**
     * Description: 根据id查询要修改的商品分类
     *
     * @author: zjf95
     * @date: 2022/4/4 9:38
     * @param:
     * @return:
     */
    @GetMapping("/{id}")
    public R One(@PathVariable Integer id) {

        log.info("更新的id为：{}", id);

        PmsProductCategory productCategory = pmsProductCategoryService.getById(id);

        if (productCategory == null) {
            return R.error("数据有误");
        }
        return R.success("操作成功", productCategory);
    }

    /**
     * Description:
     *
     * @author: zjf95
     * @date: 2022/4/4 9:39
     * @param:
     * @return:
     */
    @PostMapping("/update/{id}")
    public R update(@PathVariable Integer id, @RequestBody PmsProductCategory productCategory) {
        log.info("传输过来的id为：{}", id);
        log.info("修改的数据为：{}", productCategory);

        PmsProductCategory pmsProductCategory = pmsProductCategoryService.getById(id);
        if (pmsProductCategory == null) {
            return R.error("该数据不存在");
        }
        //修改前的上一级的id
        //Long beforeParentId = pmsProductCategory.getParentId();
        pmsProductCategoryService.updateById(productCategory);
        //修改之后的上一级id
        //Long afterParentId = productCategory.getParentId();

        return R.success("操作成功", null);
    }

    /**
     * Description: 添加商品分类
     *
     * @author: zjf95
     * @date: 2022/4/3 19:03
     * @param:
     * @return:
     */
    @PostMapping("/create")
    public R create(@RequestBody PmsProductCategory productCategory) {

        log.info("创建的数据为：{}", productCategory);

        productCategory.setLevel(0);
        productCategory.setProductCount(100);
        String name = productCategory.getName();
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(name != null, PmsProductCategory::getName, name);

        PmsProductCategory one = pmsProductCategoryService.getOne(queryWrapper);
        if (one != null) {
            return R.error("该数据已存在");
        }
        pmsProductCategoryService.save(productCategory);


        return R.success("操作成功", null);
    }

    /**
     * Description: 删除商品分类
     *
     * @author: zjf95
     * @date: 2022/4/4 9:40
     * @param:
     * @return:
     */
    /*@PostMapping("/delete/{id}")
    public R remove(@PathVariable Integer id) {
        PmsProductCategory productCategory = pmsProductCategoryService.getById(id);
        if (productCategory == null) {
            return R.error("该数据不存在");
        }

        pmsProductCategoryService.removeById(id);

        return R.success("操作成功", null);
    }*//**
     * Description: 删除商品分类
     *
     * @author: zjf95
     * @date: 2022/4/4 9:40
     * @param:
     * @return:
     */
    @PostMapping("/delete/{id}")
    public R remove(@PathVariable Integer id) {
        PmsProductCategory productCategory = pmsProductCategoryService.getById(id);
        if (productCategory == null) {
            return R.error("该数据不存在");
        }
        pmsProductCategoryService.removeById(id);

        //删除分类的时候需要删除其子类的分类
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsProductCategory::getParentId,id);

        pmsProductCategoryService.remove(queryWrapper);

        return R.success("操作成功", null);
    }

    /**
     * Description: 修改导航栏状态
     *
     * @author: zjf95
     * @date: 2022/4/4 9:40
     * @param:
     * @return:
     */
    @PostMapping("/update/navStatus")
    public R navStatus(String ids, Integer navStatus) {

        log.info("传输过来的ids为：{}", ids);
        log.info("修改的状态数据为：{}", navStatus);
        String[] split = ids.split(",");

        List<Long> bids = Arrays.stream(split).map(Long::valueOf).collect(Collectors.toList());

        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryService.listByIds(bids);

        for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
            if (pmsProductCategory != null){
                pmsProductCategory.setNavStatus(navStatus);
                pmsProductCategoryService.updateById(pmsProductCategory);
            }
        }

        return R.success("操作成功", null);
    }

    /**
     * Description: 修改显示状态
     *
     * @author: zjf95
     * @date: 2022/4/4 9:41
     * @param:
     * @return:
     */
    @PostMapping("/update/showStatus")
    public R showStatus(String ids, Integer showStatus) {

        if (StringUtils.isEmpty(ids) || ids == null) {
            return R.error("数据有误");
        }
        List<Long> bids = Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList());

        //LambdaUpdateWrapper<PmsProductCategory> updateWrapper = new LambdaUpdateWrapper<>();
        //updateWrapper.in(!bids.isEmpty(), PmsProductCategory::getId).set(PmsProductCategory::getShowStatus,showStatus);
        //
        //pmsProductCategoryService.update(updateWrapper);

        List<PmsProductCategory> pmsProductCategories = pmsProductCategoryService.listByIds(bids);

        for (PmsProductCategory pmsProductCategory : pmsProductCategories) {
            if (pmsProductCategory != null){
                pmsProductCategory.setShowStatus(showStatus);
                pmsProductCategoryService.updateById(pmsProductCategory);
            }
        }


        return R.success("操作成功", null);
    }

    @GetMapping("/list/withChildren")
    public R withChildren() throws InvocationTargetException, IllegalAccessException {
        List<PmsProductCategoryWithChildrenItem> list = pmsProductCategoryService.withChildren();
        return R.success(list);
    }

}
